package com.nf.health.app.intervention_details.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.my.MyShopStoreActivity;
import com.nf.health.app.adapter.OnMyClickListener2;
import com.nf.health.app.customview.InterventionPopupWindow;
import com.nf.health.app.http.Act;
import com.nf.health.app.intervention_details.adapter.InterventionDetailAdapter;
import com.nf.health.app.intervention_details.bean.InterventionBean;
import com.nf.health.app.intervention_details.fragment.InterventionCoverFragment;
import com.nf.health.app.intervention_details.fragment.MusicdetailsFragment;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.PreferenceHelper;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hepann on 2016/11/10.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author hepann
 * @Description
 * @email he_pann@163.com
 */

public class InterventionDetailsActivity extends BaseActivity {


    @Bind(R.id.vp_content)
    ViewPager vpContent;
    @Bind(R.id.titlebar_title_tv)
    TextView titlebarTitleTv;
    @Bind(R.id.titlebar_right_view)
    ImageView titlebarRightView;
    private InterventionDetailAdapter mVpAdapter;
    private int mFragmentIndex = 0;//当前显示的Fragment位置
//    private int mInterventionIndex = 0;//当前显示的方案位置
//    private ArrayList<String> interventions = new ArrayList<>();
    List<InterventionBean> mInterventionList;
    public static final String ID_FROM_HISTORY = "id_from_history";
    private String historyID;//记录从历史界面跳转过来的id
    private  InterventionPopupWindow pop;
    private InterventionBean currentInterventionBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.layout_common_header);
        setContent(R.layout.activity_intervention_details);
        ButterKnife.bind(this);
        historyID = getIntent().getStringExtra(ID_FROM_HISTORY);
        initView();
        initListener();
        initData();
    }

    private void initView() {
        titlebarTitleTv.setText("方案详情");
    }

    private void initListener() {
        titlebarRightView.setOnClickListener(this);
        vpContent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mFragmentIndex = position;
                if (position == 9) {
                    if (MusicdetailsFragment.player  != null) {
                        if (MusicdetailsFragment.player.mediaPlayer.isPlaying()) {
                            MusicdetailsFragment.player.stop();
                        }
                    }
                }
                if (position == 7) {
                }
                if (position == 5) {
                    if (MusicdetailsFragment.player != null) {
                        if (MusicdetailsFragment.player.mediaPlayer.isPlaying()) {
                            MusicdetailsFragment.player.stop();
                        }
                    }
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void initData() {
        provider.getInterveneList("0", "0");
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if (Act.Flag.GET_INTERVENE_LIST.equals(actionName)) {
            //方案集合
            mInterventionList = (List<InterventionBean>) object;
            if (mInterventionList.size() > 0) {
                if (TextUtils.isEmpty(historyID)) {//从首页详情过来
                    Constants.CHANGE_ABLE.INTERVENTION_ID = mInterventionList.get(0).getId();
                    PreferenceHelper.putString(PreferenceHelper.TYPE_HPID,mInterventionList.get(0).getId()+"");
                    //有数据才显示popup
                    titlebarRightView.setImageResource(R.drawable.intervention_menu);
                } else {
                    //从历史过来,只显示当前id的历史即可
                    titlebarRightView.setVisibility(View.GONE);
                    Constants.CHANGE_ABLE.INTERVENTION_ID = historyID;
                }
                //有方案就显示
                mVpAdapter = new InterventionDetailAdapter(getSupportFragmentManager());
                vpContent.setAdapter(mVpAdapter);
                currentInterventionBean = mInterventionList.get(0);
            } else {
                showToast("暂无执行方案哦");
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.titlebar_right_view:
                showPop();
                break;
            default:
                break;
        }
    }
    private void initPop(){
        if(pop == null){
          /* final WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.alpha = 0.5f;
            getWindow().setAttributes(lp);*/
            pop = new InterventionPopupWindow(this, mInterventionList, mFragmentIndex);
            //设置消失监听
            pop.setOnDismissListener(new OnPopDismissListener());
            //设置menu点击监听
            pop.setOnMyItemClickListener(new onPopMenuClickListenr());
            pop.backgroundAlpha(0.5f);
        }else{
            pop.backgroundAlpha(0.5f);
            final WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.alpha = 0.5f;
            getWindow().setAttributes(lp);
        }
    }

    /**
     * 右上角popupwindow
     */
    private void showPop() {
        initPop();
        pop.showAsDropDown(titlebarRightView);
    }

    /**
     * pop消失回调
     */
    private class OnPopDismissListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {
            pop.backgroundAlpha(1f);
        }
    }

    /**
     * pop条目点击监听
     */
    private class onPopMenuClickListenr implements OnMyClickListener2 {
        @Override
        public void onClick(int position, String flag, Object obj) {
            if ("0".equals(flag)) {
                //0 代表pop中menus条目点击,position记录点击位置
                vpContent.setCurrentItem(mFragmentIndex = position);
            } else if ("1".equals(flag)) {
                //1代表pop中其他view的点击,position是view的id
                switch (position) {
                    case R.id.tv_buy_more:
                        ActivityUtils.showActivity(InterventionDetailsActivity.this, MyShopStoreActivity.class);
                        break;
                    case R.id.tv_history:
                        ActivityUtils.showActivity(InterventionDetailsActivity.this, HistorySchemeActivity.class);
                        finish();
                        break;
                    default:
                        break;
                }
            } else if ("2".equals(flag)) {//2代表方案被点击,position是要查看的方案在集合中位置
                currentInterventionBean  = (InterventionBean) obj;
                String id = currentInterventionBean.getId();
                PreferenceHelper.putString(PreferenceHelper.TYPE_HPID,id+"");//314 315修改此处
                Constants.CHANGE_ABLE.INTERVENTION_ID = id;
                //换了新的方案就从封面开始显示
                if (mFragmentIndex == 0) {
                    InterventionDetailAdapter adapter = (InterventionDetailAdapter) vpContent.getAdapter();
                    InterventionCoverFragment fragment = (InterventionCoverFragment) adapter.instantiateItem(vpContent, 0);
                    if (fragment != null) {
                        fragment.lazyLoadData();
                    }
                } else {
                    vpContent.setCurrentItem(mFragmentIndex = 0);
                }
            }
        }
    }

    /**
     * 获取当前显示服务的hnick
     * @return
     */
    public String getHnick() {
        try {
            return currentInterventionBean.getHnick();
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }
}
